﻿#include "OverviewWidget.h"
#include "ui_OverviewWidget.h"
#include <QStackedWidget>
#include "CommonFunc.h"
#include "DataInfo.h"
#include "SetThresholdDialog.h"

OverviewWidget::OverviewWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::OverviewWidget)
{
    ui->setupUi(this);
    appendTypeToFunc(XUI_ShowThreshold, std::bind(&OverviewWidget::updateThreshold, this));
    appendTypeToFunc(XUI_ShowStatus, std::bind(&OverviewWidget::updateStatusInfo, this));
}

OverviewWidget::~OverviewWidget()
{
    delete ui;
}

void OverviewWidget::updateThreshold()
{
    static int iThreshold = -1;
    if (g_iThreshold != iThreshold)
    {
        ui->toolButton_threshold->setText(QString("%1[%2]").arg(QStringLiteral("当前转速阈值")).arg(g_iThreshold));
        iThreshold = g_iThreshold;
    }
}

void OverviewWidget::updateStatusInfo()
{
    static int iStopNum = -1, iSlowNum = -1;
    if (iStopNum != g_sensorStatus[3])
    {
        iStopNum = g_sensorStatus[3];
        ui->toolButton_stopNum->setText(QString("%1%2").arg(QStringLiteral("停转数量")).arg(g_sensorStatus[3]));
    }
    if (iSlowNum != g_sensorStatus[2])
    {
        iSlowNum = g_sensorStatus[2];
        ui->toolButton_slowNum->setText(QString("%1%2").arg(QStringLiteral("低速数量")).arg(g_sensorStatus[2]));
    }
}

void OverviewWidget::on_toolButton_exit_clicked()
{
    executeFuncFromID(XUI_Logout);
}

void OverviewWidget::on_toolButton_area_clicked()
{
    executeFuncFromID(XUI_AreaManage);
}

void OverviewWidget::on_toolButton_threshold_clicked()
{
    SetThresholdDialog dialog;
    dialog.setThresholdValue(g_iThreshold);
    dialog.setGeometry(this->x() + 70, this->y()-20 + this->height() / 3, this->width() - 70 * 2, this->height() / 3);
    dialog.exec();
    updateThreshold();    
    updateAllMessageInfo();
}
